package org.yejiali.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yejiali.common.constants.SystemConstants;
import org.yejiali.common.response.ResponseResult;
import org.yejiali.domain.entity.Article;
import org.yejiali.domain.entity.Category;
import org.yejiali.domain.vo.CategoryVo;
import org.yejiali.mapper.CategoryMapper;
import org.yejiali.service.ArticleService;
import org.yejiali.service.CategoryService;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 分类表(Category)表服务实现类
 *
 * @author yejiali
 * @since 2025-02-17 18:45:30
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private ArticleService articleService;
    @Override
    public ResponseResult getCategoryList() {
        //查询文章表状态为已发布
        //写法一
        LambdaQueryWrapper<Article> articleLambdaQueryWrapper=new LambdaQueryWrapper<>();
        articleLambdaQueryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        List<Article> articleList=articleService.list(articleLambdaQueryWrapper);
        //写法二
       /* List<Article> list = articleService.lambdaQuery()
                .eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL)
                .list();*/

        //获取文章的分类id，去重
        List<Long> categoryIds = articleList.stream()
                .map(article -> article.getCategoryId())
                .distinct()
                .collect(Collectors.toList());
        //查询分类表
        List<Category> categories = this.listByIds(categoryIds);
        //查询文章状态是否为正常
        categories= categories.stream()
                .filter(category -> SystemConstants.STATUS_NORMAL.equals(category.getStatus()))
                .collect(Collectors.toList());
        //封装vo
        List<CategoryVo> categoryVos = BeanUtil.copyToList(categories, CategoryVo.class);
        return ResponseResult.okResult(categoryVos);
    }
}


